#include "IO.h"
#include "global.h"

// int num_delete[MAX_TAG_NUM][MAX_TIME_SLICING];

void do_object_delete(const int* object_unit, int* disk_unit, int size)
{
    for (int i = 1; i <= size; i++) {
        disk_unit[object_unit[i]] = 0;
    }
}

void delete_action(int round, int time)
{
    int n_delete;
    int abort_num = 0;
    static int _id[MAX_OBJECT_NUM];

    if (round == 1) {
        n_delete=getint();// scanf("%d", &n_delete);
        n_deletes[time] = n_delete;
        for (int i = 1; i <= n_delete; i++) {
            _id[i]=getint();// scanf("%d", &_id[i]);
            delete_datas[time].push_back(_id[i]);
        }
    } else {
        n_delete = n_deletes[time];
        for (int i = 1; i <= n_delete; ++i) {
            _id[i] = delete_datas[time].at(i - 1);
        }
    }

    for (int i = 1; i <= n_delete; i++) {
        int id = _id[i];
        int current_id = object[id].last_request_point;
        while (current_id != 0) {
            if (request[current_id].is_completed() == false&&!request[current_id].isbusy) {
                abort_num++;
            }
            current_id = request[current_id].prev_id;
        }
    }

    if(Round !=2) putint(abort_num);// printf("%d\n", abort_num);
    for (int i = 1; i <= n_delete; i++) {
        int id = _id[i];
        int current_id = object[id].last_request_point;
        while (current_id != 0) {
            if (request[current_id].is_completed() == false&&!request[current_id].isbusy) {
                if(Round !=2) putint(current_id);// printf("%d\n", current_id);
            }
            current_id = request[current_id].prev_id;
        }
        for (int j = 1; j <= REP_NUM; j++) {
            do_object_delete(object[id].unit[j], disk[object[id].replica[j]], object[id].size);
        }
        object[id].is_delete = true;
    }

    if(Round !=2) fflush(stdout);
}